import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { LoginApi } from '../../api/modules/user'

export const loginAsunc = createAsyncThunk('user/loginAsunc', async (data) => {
    //api接口 等待他 
    const res = await LoginApi(data)

    return res
})

export const userSlice = createSlice({
    name: 'user',
    initialState: JSON.parse(localStorage.getItem("user")) || {
        token: "",
        userInfo: null
    },

    reducers: {

    },
    extraReducers(builder) {
        builder.addCase(loginAsunc.fulfilled, (state, action) => {
            console.log(action);
            console.log(action, "1111a1");
            if (action.payload.code == 0) {
                state.token = action.payload.token
                state.userInfo = action.payload.useInfo
                localStorage.setItem("user", JSON.stringify({ token: action.payload.token, ueseinfo: action.payload.useInfo }))
            }
        })

    }

})
export default userSlice.reducer